// components/area/area.js
import service from '../../utils/request'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
   value: {
     type: String,
     default: ''
   }
    
    
  },

  /**
   * 组件的初始数据
   */
  data: {
    province_list:null,
    province_name:null,
    addressCity:null,
    multiArray: [],  // 三维数组数据
    // multiIndex: [0, 0, 0], // selectCityId:null,
    // selectAreaId:null,
    title: "站点",
    site_name: ''
  },
  attached() {
    this.getData()
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getData() {
      let that = this
      service('GET', '/iot/sites').then((res) => {
        console.log('zhandian', res)
        let resData = [...res.data.list]  //放到一个数组里面
        let multiArray = res.data.list.map((item)=>{return item.name})  //获取名称
        let cur= ''
        let i = 0
        if(that.data.value){
          res.data.list.map((item, index)=>{
            if(that.data.value == item.id){
              cur = item
              i = index
            }
          })  //获取名称
        }
        console.log('index', i)
       if(cur){
         that.setData({
          site_name: cur.name,
          value : i
         })
       }
        resData.push({
          "id":0,
          "name":"未找到站点"
        })
        multiArray.push('未找到站点')
        console.log('resData', resData)
        that.setData({
          multiArray: multiArray, //更新三维数组  更新完为[['广东','北京'],[],[]]
          province_list: resData,  //原始数据
          province_name:multiArray,  //省级全部名称
        })
        let defaultCode = that.data.province_list[0].id //使用第一项当作参数获取市级数据
        that.setData({
          currnetProvinceId:defaultCode  //保存当前省份id
        })
      })
    },

    //picker确认选择
    confirm:function(e){
      console.log('e', e)
      this.setData({
        site_name: this.data.province_list[e.detail.value].name
      })
      console.log('site_name',this.data.site_name)
      this.triggerEvent('confirmData', this.data.province_list[e.detail.value[0]].id)
    },
  }
})
